// pages/door_service/door_service.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getStaffList, getStaffOrderList, getOrderList, staffConfirmOrder, staffFinishService, refundManage  } from '../../static/api/api.js'
//获取应用实例
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        search_type:'',
        order_type:'',
        status:'',
        memberInfo: null,
        isShowTechList:false,
        staffList:null,
        curStaffData:null,
        isNoMore: false,
        pageLoad:false,
        isShowDetail:false,
        seconds_price:'',
        seconds_remark:'',
        confirm:0,
        curItem:null,
        curIndex:'',
        cid:'',
        pages: {
            curPage: 1,
            totalPage: 1
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            'search_type': options.search_type || 1,
            'order_type': options.order_type ||10,
            'cid': options.store_user_id
        })
        this.setData({
            memberInfo: app.globalData.memberInfo
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getStaffList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    inputValueChange(e) {
        utils.inputValueChange(e, this)
    },
    showTechList(){
        this.setData({
            isShowTechList:true
        })
    },
    hideTechList() {
        this.setData({
            isShowTechList: false
        })
    },
    lower(){
        this.getStaffOrderList()
    },
    //用户接单操作
    staffConfirmOrder(e){
        let that = this;
        let dataset = e.currentTarget.dataset;
        let msg='';
        if(dataset.type == 1){
            msg ="确定服务当前订单吗？";
        }else{
            msg = "确定拒绝当前订单吗？";
        }
        wx.showModal({
            title: '提示',
            content: msg,
            success(res) {
                if (res.confirm) {
                    let params = {
                        order_id: dataset.item.id,
                        receive: dataset.type
                    }
                    wx.showLoading({
                        title: '加载中...',
                    })
                    staffConfirmOrder(params).then(response => {
                        wx.hideLoading()
                        if (dataset.type == 1) {
                            wx.showToast({
                                title: '确认接单成功',
                            })
                        } else {
                            wx.showToast({
                                title: '拒绝成功',
                            })
                        }
                        that.data.orderList.splice(dataset.index, 1)
                        that.setData({
                            'orderList': that.data.orderList
                        })
                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {

                }
            }
        })
        
    },
    //获取员工列表
    getStaffList() {
        let that = this;
        getStaffList().then(response => {
            let cData = response.data[0]
            if(this.data.cid){
                response.data.forEach((el,index)=>{
                    if(el.id == this.data.cid){
                        cData=el;
                    }
                })
            }
            that.setData({
                'staffList': response.data,
                'curStaffData': cData
            })
            this.getStaffOrderList()
        }).catch(response => {

        })
    },
    //获取员工订单
    getStaffOrderList(){
        let that = this;
        if (this.data.pageLoad) return;
        if (this.data.pages.curPage > this.data.pages.totalPage) return;
        this.setData({
            pageLoad: true
        })
        let params = {
            search_type: this.data.search_type,
            status:this.data.status,
            order_type:this.data.order_type,
            store_user_id:this.data.curStaffData.id,
            page: this.data.pages.curPage
        }
        getStaffOrderList(params).then(response => {
            
            let data = response.data;
            this.setData({
                'confirm': response.confirm || ''
            })
            if (that.data.pages.curPage == 1) {
                that.setData({
                    orderList: data.lists
                })
            } else {
                that.setData({
                    orderList: that.data.orderList.concat(data.lists)
                })
            }
            that.setData({
                'pages.curPage': that.data.pages.curPage + 1,
                'pages.totalPage': data.page.total_page,
                'pageLoad': false
            })
            if (this.data.pages.curPage >= this.data.pages.totalPage) {
                this.setData({
                    isNoMore: true
                })
            };
        }).catch(response => {
            this.setData({
                'pageLoad': false
            })
        })
    },
    staffClickChange(e) {
        let dataset = e.currentTarget.dataset;
        this.setData({
            curStaffData:this.data.staffList[dataset.index]
        })
        this.setData({
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'orderList':null,
            'isShowTechList':false
        })
        this.getStaffOrderList()
    },
    changeStatus(e){
        let dataset = e.currentTarget.dataset;

        this.setData({
            'status': dataset.status,
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'orderList': null
        })
        this.getStaffOrderList()
    },
    hideAddOrderr(){
        this.setData({
            'isShowDetail':false
        })
    },
    showAddOrderr(e) {
        this.setData({
            'isShowDetail': true
        })
    },
    comfirmServiceOver(e){
        let dataset = e.currentTarget.dataset;
        let that = this;
        wx.showModal({
            title: '提示',
            content: '请确定当前服务是否完成',
            success(res) {
                if (res.confirm) {
                    that.setData({
                        'curItem':dataset.item,
                        'curIndex':dataset.index
                    })
                    that.showAddOrderr(e)
                } else if (res.cancel) {

                }
            }
        })
        
    },
    //员工确认服务是否完成
    staffFinishService(e) {
        let dataset = e.currentTarget.dataset;
        let that = this;
        let params={
            order_id: dataset.item.id,
            is_seconds: dataset.is_seconds==1?false:true,
            seconds_price: this.data.seconds_price,
            seconds_remark: this.data.seconds_remark
        }
        if (dataset.is_seconds == 2){
            if (this.data.seconds_price==''){
                wx.showToast({
                    icon:'none',
                    title: '请输入加单金额',
                })
                return;
            }
            if (this.data.seconds_remark == '') {
                wx.showToast({
                    icon:'none',
                    title: '请输入加单备注',
                })
                return;
            }
        }
        wx.showLoading({
            title: '加载中...',
        })
        staffFinishService(params).then(response => {
            wx.hideLoading()
            wx.showToast({
                icon:'none',
                title: '操作成功，请等待用户确认',
            })
            that.data.orderList.splice(that.data.curIndex, 1)
            that.setData({
                'orderList': that.data.orderList,
                'isShowDetail': false
            })
        }).catch(response => {
            wx.hideLoading()
        })
    },
    refundManage(e) {
        let that = this;
        let dataset = e.currentTarget.dataset;
        let msg = '';
        if (dataset.type == 1) {
            msg = "确定同意当前退款吗？";
        } else {
            msg = "确定拒绝当前退款吗？";
        }
        wx.showModal({
            title: '提示',
            content: msg,
            success(res) {
                if (res.confirm) {
                    let params = {
                        order_id: dataset.item.id,
                        receive: dataset.type
                    }
                    wx.showLoading({
                        title: '加载中...',
                    })
                    refundManage(params).then(response => {
                        wx.hideLoading()
                        if (dataset.type == 1) {
                            wx.showToast({
                                title: '操作成功',
                            })
                        } else {
                            wx.showToast({
                                title: '操作成功',
                            })
                        }
                        that.data.orderList.splice(dataset.index, 1)
                        that.setData({
                            'orderList': that.data.orderList
                        })
                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {

                }
            }
        })
    },
    makePhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone,
        })
    }
    
})